package com.smartkey.adapter;

import java.util.ArrayList;

import com.project.smartkey.R;
import com.smartkey.bean.AppInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainAppListAdapter extends BaseAdapter {
	private ArrayList<AppInfo> appList;
	private Context context;
	private LayoutInflater inflater;
	private String check;

	public MainAppListAdapter(Context ctx, ArrayList<AppInfo> list,String ck) {
		this.appList = list;
		this.context = ctx;
		this.check = ck;
		inflater = LayoutInflater.from(context);
	}

	public void resetData(String ck) {
		this.check = ck;
	}

	@Override
	public int getCount() {
		return appList.size();
		
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		AppInfo appInfo = appList.get(arg0);

		Holder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.main_app_list_item, null);
			holder = getHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.icon.setBackgroundDrawable(appInfo.appIcon);
		holder.name.setText(appInfo.appName);

//		System.out.println("########### appInfo.appName="+appInfo.appName);
//		System.out.println("########### appInfo.isCheck="+appInfo.isCheck);
		// 设置是否选中
		if(check==null||check.equals("")){
			if(arg0==0){
				holder.check.setVisibility(View.VISIBLE);
			}else{
				holder.check.setVisibility(View.GONE);
			}
		}else{
			if (appInfo.packageName.equals(check)) {
				holder.check.setVisibility(View.VISIBLE);
			} else {
				holder.check.setVisibility(View.GONE);
			}
		}
		return convertView;
	}

	private Holder getHolder(View convertView) {
		Holder holder = new Holder();
		holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
		holder.check = (ImageView) convertView.findViewById(R.id.app_check);
		holder.name = (TextView) convertView.findViewById(R.id.app_name);
		return holder;
	}

	private class Holder {
		ImageView icon;
		TextView name;
		ImageView check;
	}

}
